<template>
  <div class="equity-Container">
    <div class="head">
      <div class="search">
        <el-input
          placeholder="请输入项目名称,项目代币"
          suffix-icon="el-icon-search"
          v-model="search"
          @keyup.enter.native="getList()"
        ></el-input>
      </div>
    </div>

    <el-table :data="List" border style="width: 100%">
      <el-table-column prop="index" label="序号" width="120"></el-table-column>

      <el-table-column prop="projectsName" label="项目名称"></el-table-column>

      <el-table-column prop="moneyName" label="项目代币"></el-table-column>

      <el-table-column prop="numberSum" label="发行总额"></el-table-column>

      <el-table-column prop="bonusMoney" label="分红票房(MOV)"></el-table-column>

      <el-table-column prop="residueMoney" label="待分配(MOV)"></el-table-column>

      <el-table-column prop="grantMoney" label="已分配(MOV)"></el-table-column>

      <el-table-column label="数据有效性">
        <template slot-scope="scope">
          <el-button
            type="text"
            v-show="scope.row.type == 0"
            @click.native="pendding(scope.row)"
          >待审核</el-button>
          <el-button type="text" v-show="scope.row.type == 1">有效</el-button>
          <el-button type="text" v-show="scope.row.type == 2">无效</el-button>
        </template>
      </el-table-column>

      <el-table-column label="权益发放状态">
        <template slot-scope="scope">
          <el-button type="text" v-show="scope.row.bonusStatus == 0">未发放</el-button>
          <el-button type="text" v-show="scope.row.bonusStatus == 1">部分发放</el-button>
          <el-button type="text" v-show="scope.row.bonusStatus == 2">已发放</el-button>
        </template>
      </el-table-column>

      <el-table-column label="操作" width="250">
        <template slot-scope="scope">
          <el-button type="text" @click.native="goInfo(scope.row)">详情</el-button>
          <el-button type="text" @click="equityDelete(scope.row)">删除</el-button>
          <el-button
            type="text"
            @click.native="auditClick(scope.row)"
            v-show="scope.row.type == 0"
          >审核</el-button>
          <el-button
            type="text"
            @click.native="equityGrant(scope.row)"
            v-show="scope.row.bonusStatus !== 2"
          >权益发放</el-button>
        </template>
      </el-table-column>
    </el-table>

    <div class="paginat">
      <el-pagination
        @current-change="handleCurrentChange"
        :current-page.sync="currentPage4"
        :page-size="10"
        layout="total, prev, pager, next"
        :total="total"
      ></el-pagination>
    </div>

    <!-- 审核 -->
    <el-dialog title="提示" width="30%" :visible.sync="outerVisible">
      是否对用户： {{projectsName}} , 进行审核?
      <el-dialog width="20%" title="审核" :visible.sync="innerVisible" append-to-body>请选择审核状态
        <div slot="footer" class="dialog-footer">
          <el-button @click="auditFalse()">无效</el-button>
          <el-button type="primary" @click="auditOk()">有效</el-button>
        </div>
      </el-dialog>
      <div slot="footer" class="dialog-footer">
        <el-button @click="outerVisible = false">取 消</el-button>
        <el-button type="primary" @click="innerVisible = true">开始审核</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
      dialogVisible: false,
      search: "",
      total: 0,
      val: 1, //当前多少页
      currentPage4: 1,
      List: [],
      projectsId: "",
      projectsName: "",
      outerVisible: false,
      innerVisible: false
    };
  },
  created() {
    this.getList();
  },
  methods: {
    //删除
    equityDelete(row) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          let url = "/bonus/edit";
          let params = {
            id: row.id,
            status: 1
          };
          this.httpPost(url, params, res => {
            if (res.code == 200) {
              this.$message({
                type: "success",
                message: "删除成功"
              });
              this.getList();
            } else {
              this.$message({
                type: "warning",
                message: res.message
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    //发放
    equityGrant(row) {
      this.$confirm(
        "是否对: " + row.projectsName + " , 进行权益发放?",
        "权益发放",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消"
        }
      )
        .then(() => {
          let url = "/bonus/grantAll/" + row.id;
          this.httpGet(url, res => {
            if (res.code == 200) {
              this.$message({
                type: "success",
                message: "权益发放成功"
              });
              this.getList();
            } else {
              this.$message({
                type: "warning",
                message: res.message
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消权益发放"
          });
        });
    },
    //审核通过
    auditOk() {
      let url = "/bonus/valid";
      let params = {
        type: 1,
        id: this.projectsId
      };
      this.httpPost(url, params, res => {
        if (res.code == 200) {
          this.$message({
            type: "success",
            message: "通过"
          });
          this.innerVisible = false;
          this.outerVisible = false;
          this.getList();
        } else {
          this.$message({
            type: "warning",
            message: res.message
          });
        }
      });
    },
    //审核不通过
    auditFalse() {
      let url = "/cash/update";
      let params = {
        type: 2,
        id: this.projectsId
      };
      this.httpPost(url, params, res => {
        if (res.code == 200) {
          this.$message({
            type: "info",
            message: "不通过"
          });
          this.innerVisible = false;
          this.outerVisible = false;
          this.getList();
        } else {
          this.$message({
            type: "warning",
            message: res.message
          });
        }
      });
    },
    //审核
    auditClick(row) {
      this.projectsName = row.projectsName;
      this.outerVisible = true;
      this.projectsId = row.id;
    },
    //去详情
    goInfo(row) {
      this.$router.push("/equityInfo?id=" + row.id);
    },
    //审核
    pendding(row) {
      this.$confirm("是否开始审核？", "审核", {
        distinguishCancelAndClose: true,
        confirmButtonText: "有效",
        cancelButtonText: "无效"
      })
        .then(() => {
          let url = "/bonus/valid";
          let params = {
            id: row.id,
            type: 1
          };
          this.httpPost(url, params, res => {
            if (res.code == 200) {
              this.$message({
                type: "success",
                message: "有效"
              });
              this.getList();
            }
          });
        })
        .catch(action => {
          if (action === "cancel") {
            let url = "/bonus/valid";
            let params = {
              id: row.id,
              type: 2
            };
            this.httpPost(url, params, res => {
              if (res.code == 200) {
                this.$message({
                  type: "info",
                  message: "无效"
                });
                this.getList();
              }
            });
          }
        });
    },
    //分页
    handleCurrentChange(val) {
      this.val = val;
      this.getList();
      console.log(`当前页: ${val}`);
    },
    //
    getList() {
      let url = "/bonus/bonusList";
      let params = {
        pageNum: this.val,
        pageSize: 10,
        key: this.search
      };
      this.httpPost(url, params, res => {
        if (res.code == 200) {
          this.List = res.data.list;
          this.total = res.data.total;
          this.List.forEach((element, i) => {
            this.List[i].index = (this.val - 1) * 10 + i + 1;
          });
        }
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.head {
  height: 60px;
  .search {
    width: 300px;
    height: 60px;
    float: right;
  }
}
</style>

<style lang="scss">
.equity-Container {
  .el-button + .el-button {
    margin-left: 0px !important;
  }
}
</style>
